﻿using System;
using System.DirectoryServices;

namespace zielonka.co.uk.extensions.system.directoryservices
{
    // Description:	extensions that work with SearchResult to retrieve its data.
    /*
        result.GetValue("sAMAccountName", String.Empty)
     */

    public static partial class SearchResultExtensions
    {
        public static T GetValue<T>(this SearchResult result, string key)
        {
            return result.GetValue(key, default(T));
        }
        public static T GetValue<T>(this SearchResult result, string key, T defaultValue)
        {
            if (!result.Properties.Contains(key))
            {
                return defaultValue;
            }

            var resultPropertyValueCollection = result.Properties[key];
            if (resultPropertyValueCollection.Count == 0)
            {
                return defaultValue;

            }

            return (T)resultPropertyValueCollection[0];
        }
    }
}
